package com.ysy.tz.mfg.controller;

import com.google.common.eventbus.Subscribe;
import com.ysy.tz.core.annotation.Service;
import com.ysy.tz.core.engine.ApplicationContext;
import com.ysy.tz.core.engine.ConfigLoader;
import com.ysy.tz.core.engine.CustomerFXMLLoader;
import com.ysy.tz.core.engine.KeyValuePair;
import com.ysy.tz.core.engine.Modelable;
import com.ysy.tz.core.engine.ParameterInitializable;
import com.ysy.tz.core.event.BizEvent;
import com.ysy.tz.core.selfcomp.BarCodeImageView;
import com.ysy.tz.core.selfcomp.CommonDialog;
import com.ysy.tz.core.selfcomp.CustomerAlert;
import com.ysy.tz.core.selfcomp.CustomerButton;
import com.ysy.tz.core.selfcomp.CustomerComboBox;
import com.ysy.tz.core.selfcomp.CustomerDialogPane;
import com.ysy.tz.core.selfcomp.CustomerToggleButton;
import com.ysy.tz.core.selfcomp.PopupTextField;
import com.ysy.tz.core.selfcomp.QRCodeImageView;
import com.ysy.tz.core.selfcomp.TextChangeListener;
import com.ysy.tz.core.utils.FileResourceLoader;
import com.ysy.tz.core.utils.PrintUtils;
import com.ysy.tz.core.utils.SQLUtils;
import com.ysy.tz.core.utils.SessionUtils;
import com.ysy.tz.mfg.component.PigGradingAction;
import com.ysy.tz.mfg.component.PigGradingNode;
import com.ysy.tz.mfg.component.PigGradingOldNode;
import com.ysy.tz.mfg.component.PigGradingPane;
import com.ysy.tz.mfg.dao.GradingDetailDao;
import com.ysy.tz.mfg.dao.GradingRuleDao;
import com.ysy.tz.mfg.dao.SupplierDao;
import com.ysy.tz.mfg.dao.SysMachineParamDao;
import com.ysy.tz.mfg.dao.WarehouseDao;
import com.ysy.tz.mfg.entity.GradingDetailEntity;
import com.ysy.tz.mfg.entity.GradingRuleEntity;
import com.ysy.tz.mfg.entity.GradingRuleGroup;
import com.ysy.tz.mfg.entity.SupplierEntity;
import com.ysy.tz.mfg.entity.SysMachineParamEntity;
import com.ysy.tz.mfg.entity.UserEntity;
import com.ysy.tz.mfg.entity.WarehouseEntity;
import com.ysy.tz.mfg.equipment.scale.ToledoScale;
import com.ysy.tz.mfg.equipment.scale.WeightListener;
import com.ysy.tz.mfg.service.PigGradingService;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.ResourceBundle;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.regex.Pattern;
import javafx.animation.Interpolator;
import javafx.animation.PathTransition;
import javafx.animation.PathTransition.OrientationType;
import javafx.application.Platform;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.TextInputControl;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.input.PickResult;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.paint.Color;
import javafx.scene.shape.HLineTo;
import javafx.scene.shape.MoveTo;
import javafx.scene.shape.Path;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.util.Duration;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.ProcessingInstruction;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.dom4j.tree.DefaultProcessingInstruction;

public class PigGradingController implements ParameterInitializable {
    @FXML
    private PigGradingPane pigPane;
    @FXML
    private Label supplierLabel;
    @FXML
    private Label seqNoLabel;
    @FXML
    private Label barcodeLabel;
    @FXML
    private Label weightLabel;
    @FXML
    private CustomerToggleButton bapiBtn;
    @FXML
    private CustomerToggleButton tuimaoBtn;
    @FXML
    private CustomerToggleButton maleBtn;
    @FXML
    private CustomerToggleButton femaleBtn;
    @FXML
    private ToggleButton pinkongchangeLevelBtn;
    @FXML
    private ToggleButton changeLevelBtn;
    @FXML
    private ToggleButton carcassBtn;
    @FXML
    private ToggleButton maoqiangBtn;
    @FXML
    private ToggleButton jingqiangBtn;
    @FXML
    private Pane loopPigPane;
    @FXML
    private PopupTextField supplierTextField;
    @FXML
    private TextField billNoTextField;
    @FXML
    private TextField barcodeTextField;
    @FXML
    private TextField weightTextField;
    @FXML
    private PopupTextField warehouseTextField;
    @FXML
    private HBox gradingRuleBox;
    HashMap<String, GradingRuleGroup> gradingRuleGroups = new HashMap();
    @Service
    PigGradingService pigGradingService;
    DecimalFormat decimalFormat = new DecimalFormat("00000");
    DecimalFormat weightFormat = new DecimalFormat("00.00");
    private Integer maxSerialNum;
    private String maxBillNo;
    private Pane gradingPrintRegion;
    private String oldQRValue;
    @FXML
    private Button printBtn;
    ToledoScale toledoScale = new ToledoScale();
    private LinkedBlockingQueue printQueue = new LinkedBlockingQueue();
    ImitateWorker currentImitateWorker;
    int printStopTime;
    int printDpi;
    int paperWidthMM;
    int paperHeightMM;
    PrintWorkThread printWorkThread = new PrintWorkThread();
    CustomerDialogPane changeLevelDialogPane = null;
    Label supplierNameLabel = null;
    Label billNoLabel = null;
    Label weightValueLabel = null;
    FlowPane flowPane = null;
    private PigGradingNode changePigGradingNode;
    CustomerDialogPane changeGradingLevelAndPrint = null;
    PathTransition pathTransition = null;

    public PigGradingController() {
    }

    public void initialize(URL location, ResourceBundle resources, Object... params) {
        try {
            ImageView imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_supplier.png")));
            imageView.setFitWidth(18.0);
            imageView.setFitHeight(18.0);
            this.supplierLabel.setGraphic(imageView);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_seqno.png")));
            imageView.setFitWidth(28.0);
            imageView.setFitHeight(28.0);
            this.seqNoLabel.setGraphic(imageView);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_barcode.png")));
            imageView.setFitWidth(18.0);
            imageView.setFitHeight(18.0);
            this.barcodeLabel.setGraphic(imageView);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_weightlabel.png")));
            imageView.setFitWidth(18.0);
            imageView.setFitHeight(18.0);
            this.weightLabel.setGraphic(imageView);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_bapilabel.png")));
            this.bapiBtn.setNormalImage(imageView);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_bapilabel2.png")));
            this.bapiBtn.setSelectImage(imageView);
            this.bapiBtn.styleProperty().bind(Bindings.when(this.bapiBtn.selectedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 20 0 0 20;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 20 0 0 20;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            this.bapiBtn.setSelected(true);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_tuimaolabel.png")));
            this.tuimaoBtn.setNormalImage(imageView);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_tuimaolabel2.png")));
            this.tuimaoBtn.setSelectImage(imageView);
            this.tuimaoBtn.styleProperty().bind(Bindings.when(this.tuimaoBtn.selectedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 0 20 20 0;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 0 20 20 0;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_malelabel.png")));
            this.maleBtn.setNormalImage(imageView);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_malelabel2.png")));
            this.maleBtn.setSelectImage(imageView);
            this.maleBtn.styleProperty().bind(Bindings.when(this.maleBtn.selectedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 20 0 0 20;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 20 0 0 20;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            this.maleBtn.setSelected(true);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_malelabel.png")));
            this.femaleBtn.setNormalImage(imageView);
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_malelabel2.png")));
            this.femaleBtn.setSelectImage(imageView);
            this.femaleBtn.styleProperty().bind(Bindings.when(this.femaleBtn.selectedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 0 20 20 0;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 0 20 20 0;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            this.femaleBtn.selectedProperty().addListener((observable, oldValue, newValue) -> {
                if (newValue) {
                    this.jingqiangBtn.setSelected(true);
                    this.maoqiangBtn.setDisable(true);
                } else {
                    this.maoqiangBtn.setDisable(false);
                }

            });
            new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_changeSeqlabel.png")));
            this.pinkongchangeLevelBtn.styleProperty().bind(Bindings.when(this.pinkongchangeLevelBtn.pressedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 20 20 20 20;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 20 20 20 20;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            imageView = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_changeLevellabel.png")));
            this.changeLevelBtn.setGraphic(imageView);
            this.changeLevelBtn.styleProperty().bind(Bindings.when(this.changeLevelBtn.selectedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 20 20 20 20;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 20 20 20 20;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            this.carcassBtn.styleProperty().bind(Bindings.when(this.carcassBtn.pressedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 20 20 20 20;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 20 20 20 20;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            this.printBtn.styleProperty().bind(Bindings.when(this.printBtn.pressedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 20 20 20 20;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 20 20 20 20;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            this.maoqiangBtn.styleProperty().bind(Bindings.when(this.maoqiangBtn.selectedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 20 0 0 20;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 20 0 0 20;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            this.jingqiangBtn.styleProperty().bind(Bindings.when(this.jingqiangBtn.selectedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 0 20 20 0;-fx-border-radius: 20;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);").otherwise("-fx-background-radius: 0 20 20 0;-fx-border-radius: 20;-fx-background-color: rgb(233,233,233);-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 2, 0, 0, 0);"));
            this.jingqiangBtn.setSelected(true);
            ImageView looppigview = new ImageView(new Image(FileResourceLoader.loadInputStream("/views/images/piggrading_looppigpane.png")));
            this.loopPigPane.getChildren().add(looppigview);
            this.startLoopPane(this.loopPigPane, looppigview);
            this.supplierTextField.addTextChangeListener(new TextChangeListener<SupplierEntity>() {
                public ObservableList textChanged(TextInputControl textField, String newValue) {
                    return (ObservableList)SQLUtils.executeSQLWithReturn(SupplierDao.class, (supplierDao) -> {
                        try {
                            ArrayList<SupplierEntity> supplierEntityList = supplierDao.searchSupplierList(ApplicationContext.enterpriseId, newValue);
                            return FXCollections.observableList(supplierEntityList);
                        } catch (Exception var3) {
                            var3.printStackTrace();
                            return null;
                        }
                    });
                }

                public void PopupChoise(final SupplierEntity supplierEntity) {
                    PigGradingController.this.supplierTextField.setModel(supplierEntity);
                    Platform.runLater(new Runnable() {
                        public void run() {
                            String mBillNo = (String)SQLUtils.executeSQLWithReturn(GradingDetailDao.class, (gradingDetailDao) -> {
                                String workDay = String.valueOf(SessionUtils.getAttribute(SessionUtils.WORK_DAY));
                                return gradingDetailDao.selectLastBillNoWithDay(ApplicationContext.enterpriseId, supplierEntity.getSupplierId(), workDay);
                            });
                            if (StringUtils.isNotBlank(mBillNo)) {
                                PigGradingController.this.billNoTextField.setText(mBillNo);
                            } else {
                                PigGradingController.this.billNoTextField.setText(PigGradingController.this.getMaxBillNo(mBillNo, supplierEntity.getSupplierCode()));
                            }

                        }
                    });
                }
            });
            String warehouseCode = ConfigLoader.readDefaultChoiseConfigProperties("pigGradingInWarehouseCode");
            if (StringUtils.isNotBlank(warehouseCode)) {
                SQLUtils.executeSQL(WarehouseDao.class, (warehouseDao) -> {
                    try {
                        WarehouseEntity warehouseEntity = new WarehouseEntity();
                        int enterpriseId = ((UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER)).getEnterpriseId();
                        warehouseEntity.setEnterpriseId(enterpriseId);
                        ArrayList<WarehouseEntity> warehouseEntities = warehouseDao.searchWarehouseByCode(enterpriseId, warehouseCode);
                        if (CollectionUtils.isNotEmpty(warehouseEntities)) {
                            this.warehouseTextField.setModel((Modelable)warehouseEntities.get(0));
                        }
                    } catch (Exception var6) {
                        var6.printStackTrace();
                    }

                });
            }

            this.warehouseTextField.addTextChangeListener(new TextChangeListener<WarehouseEntity>() {
                public ObservableList textChanged(TextInputControl textField, String newValue) {
                    return (ObservableList)SQLUtils.executeSQLWithReturn(WarehouseDao.class, (warehouseDao) -> {
                        try {
                            WarehouseEntity warehouseEntity = new WarehouseEntity();
                            int enterpriseId = ((UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER)).getEnterpriseId();
                            warehouseEntity.setEnterpriseId(enterpriseId);
                            warehouseEntity.setKeyWord(newValue);
                            ArrayList<WarehouseEntity> warehouseEntities = warehouseDao.listForPage(warehouseEntity);
                            return FXCollections.observableList(warehouseEntities);
                        } catch (Exception var5) {
                            var5.printStackTrace();
                            return null;
                        }
                    });
                }

                public void PopupChoise(WarehouseEntity warehouseEntity) {
                    PigGradingController.this.warehouseTextField.setModel(warehouseEntity);
                    ConfigLoader.saveDefaultChoiseConfigProperties("pigGradingInWarehouseCode", warehouseEntity.getWarehouseCode());
                }
            });
            ArrayList<GradingRuleEntity> gradingRuleEntities = (ArrayList)SQLUtils.executeSQLWithReturn(GradingRuleDao.class, (gradingRuleDao) -> {
                try {
                    return gradingRuleDao.searchGradingRuleList(ApplicationContext.enterpriseId);
                } catch (Exception var2) {
                    var2.printStackTrace();
                    return null;
                }
            });
            Font font = new Font(16.0);
            if (CollectionUtils.isNotEmpty(gradingRuleEntities)) {
                GradingRuleEntity gradingRuleEntity;
                GradingRuleGroup gradingRuleGroup;
                for(Iterator var9 = gradingRuleEntities.iterator(); var9.hasNext(); gradingRuleGroup.getGradingRuleEntitys().add(gradingRuleEntity)) {
                    gradingRuleEntity = (GradingRuleEntity)var9.next();
                    gradingRuleGroup = (GradingRuleGroup)this.gradingRuleGroups.get(gradingRuleEntity.getLevelCode());
                    if (gradingRuleGroup == null) {
                        gradingRuleGroup = new GradingRuleGroup(gradingRuleEntity.getLevelCode());
                        this.gradingRuleGroups.put(gradingRuleEntity.getLevelCode(), gradingRuleGroup);
                    }
                }

                this.gradingRuleGroups.forEach((key, gradingRuleGroupx) -> {
                    CustomerButton button = new CustomerButton();
                    button.setModel(gradingRuleGroupx);
                    button.prefWidth(50.0);
                    button.prefHeight(50.0);
                    button.setStyle("-fx-background-radius: 25;-fx-border-radius: 25;");
                    button.styleProperty().bind(Bindings.when(button.focusedProperty()).then("-fx-background-color: rgb(203,232,203);-fx-background-radius: 25;-fx-border-radius: 25;-fx-effect: dropshadow(gaussian, rgba(0,0,0,0.5), 5, 0, 0, 0);").otherwise("-fx-background-radius: 25;-fx-border-radius: 25;"));
                    button.setFont(font);
                    this.gradingRuleBox.getChildren().add(button);
                });
            }

            String workDay = String.valueOf(SessionUtils.getAttribute(SessionUtils.WORK_DAY));
            this.maxSerialNum = (Integer)SQLUtils.executeSQLWithReturn(GradingDetailDao.class, (gradingDetailDao) -> {
                return gradingDetailDao.selectMaxSerialNumWithDay(ApplicationContext.enterpriseId, workDay);
            });

            try {
                ArrayList<SysMachineParamEntity> sysMachineParamEntities = (ArrayList)SQLUtils.executeSQLWithReturnException(SysMachineParamDao.class, (sysMachineParamDao) -> {
                    return sysMachineParamDao.searchSysMachineParamList("grading", "toledo", "weight");
                });
                sysMachineParamEntities.forEach((sysMachineParamEntity) -> {
                    if (sysMachineParamEntity.getKeyPair().equals("baudrate")) {
                        this.toledoScale.setBaudrate(Integer.parseInt(sysMachineParamEntity.getPosParaValue()));
                    } else if (sysMachineParamEntity.getKeyPair().equals("port")) {
                        this.toledoScale.setPortName(sysMachineParamEntity.getPosParaValue());
                    } else if (sysMachineParamEntity.getKeyPair().equals("point")) {
                        this.toledoScale.setFractionDigits(Integer.parseInt(sysMachineParamEntity.getPosParaValue()));
                    } else if (sysMachineParamEntity.getKeyPair().equals("databits")) {
                        this.toledoScale.setDataBits(Integer.parseInt(sysMachineParamEntity.getPosParaValue()));
                    } else if (sysMachineParamEntity.getKeyPair().equals("stopbits")) {
                        this.toledoScale.setStopBits(Integer.parseInt(sysMachineParamEntity.getPosParaValue()));
                    } else if (sysMachineParamEntity.getKeyPair().equals("parity")) {
                        this.toledoScale.setParity(Integer.parseInt(sysMachineParamEntity.getPosParaValue()));
                    } else if (sysMachineParamEntity.getKeyPair().equals("sameCount")) {
                        this.toledoScale.setSameCount(Integer.parseInt(sysMachineParamEntity.getPosParaValue()));
                    } else if (sysMachineParamEntity.getKeyPair().equals("minWeight")) {
                        this.toledoScale.setMinWeight(new BigDecimal(sysMachineParamEntity.getPosParaValue()));
                    } else if (sysMachineParamEntity.getKeyPair().equals("startPoint")) {
                        this.toledoScale.setStartPoint(Integer.parseInt(sysMachineParamEntity.getPosParaValue()));
                    } else if (sysMachineParamEntity.getKeyPair().equals("endPoint")) {
                        this.toledoScale.setEndPoint(Integer.parseInt(sysMachineParamEntity.getPosParaValue()));
                    } else if (sysMachineParamEntity.getKeyPair().equals("errorRange")) {
                        this.toledoScale.setErrorRange(new BigDecimal(sysMachineParamEntity.getPosParaValue()));
                    }

                });
                this.toledoScale.addWeightListener(new WeightListener() {
                    public void obtainWeight(String weightStr) {
                        if (StringUtils.isNotBlank(weightStr) && Float.parseFloat(weightStr) != 0.0F) {
                            System.out.println("------------->>>重量稳定---->>>" + weightStr);
                            Platform.runLater(() -> {
                                if (PigGradingController.this.check()) {
                                    System.out.println("---------------->>>>> 增加一头!!!");
                                    double weight = Double.valueOf(weightStr);
                                    PigGradingNode pigGradingNode;
                                    if (weight < 3.0) {
                                        pigGradingNode = new PigGradingNode();
                                        pigGradingNode.setEmpty(true);
                                        PigGradingController.this.addEmptyWeightPigGradingNode(pigGradingNode, new BigDecimal(weightStr));
                                    } else {
                                        pigGradingNode = new PigGradingNode();
                                        PigGradingController.this.addEmptyWeightPigGradingNode(pigGradingNode, new BigDecimal(weightStr));
                                    }
                                }

                            });
                        }

                    }
                });
                this.toledoScale.connect();
            } catch (Exception var12) {
                var12.printStackTrace();
            }
        } catch (IOException var13) {
            var13.printStackTrace();
        }

        this.initGradingPrint();
        this.printWorkThread.start();
    }

    public void unload() {
        if (this.currentImitateWorker != null) {
            this.currentImitateWorker.stop();
        }

        this.pathTransition.stop();
        this.currentImitateWorker = null;
        this.pathTransition = null;
        this.printQueue = null;
        this.printWorkThread.interrupt();
        this.printWorkThread = null;
        System.gc();
    }

    private boolean check() {
        if (null == this.supplierTextField.getModel()) {
            this.supplierTextField.requestFocus();
            return false;
        } else if (null == this.warehouseTextField.getModel()) {
            (new CustomerAlert(AlertType.ERROR, "请先选择库房!", new ButtonType[0])).showAndWait();
            this.warehouseTextField.requestFocus();
            return false;
        } else {
            return true;
        }
    }

    public void changeImitate() {
        if (this.carcassBtn.isSelected()) {
            if (this.check()) {
                System.out.println("----------开始模拟加猪------->>");
                this.carcassBtn.setText("停止模拟");
                this.currentImitateWorker = new ImitateWorker(2000);
                this.currentImitateWorker.start();
            } else {
                this.carcassBtn.setText("开始模拟");
                this.carcassBtn.setSelected(false);
            }
        } else {
            System.out.println("----------停止模拟加猪------->>");
            this.carcassBtn.setText("开始模拟");
            this.currentImitateWorker.stop();
            this.currentImitateWorker = null;
        }

    }

    public void testPrint() {
        CustomerDialogPane dialogPane = new CustomerDialogPane();
        dialogPane.setPrefSize(480.0, 120.0);
        GridPane gridPane = new GridPane();
        gridPane.setHgap(5.0);
        gridPane.setVgap(15.0);
        Label label = new Label("等  级");
        label.setAlignment(Pos.CENTER);
        label.setStyle("-fx-font-size: 20;-fx-font-weight: bold;");
        label.setPrefWidth(140.0);
        CustomerComboBox customerComboBox = new CustomerComboBox();
        customerComboBox.setStyle("-fx-font-size: 20;-fx-font-weight: bold;-fx-pref-width: 250;");
        customerComboBox.setItems(FXCollections.observableArrayList(this.gradingRuleGroups.values()));
        gridPane.add(label, 0, 0);
        gridPane.add(customerComboBox, 1, 0);
        label = new Label("打印次数");
        label.setAlignment(Pos.CENTER);
        label.setStyle("-fx-font-size: 20;-fx-font-weight: bold;");
        label.setPrefWidth(140.0);
        TextField textField = new TextField();
        textField.setStyle("-fx-font-size: 20;-fx-font-weight: bold; -fx-pref-width: 250;");
        gridPane.add(label, 0, 1);
        gridPane.add(textField, 1, 1);
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        hBox.setSpacing(10.0);
        Button okBtn = new Button("打印");
        okBtn.setPrefWidth(120.0);
        okBtn.setStyle("-fx-font-size: 20;-fx-font-weight: bold;");
        Button cancelBtn = new Button("取消");
        cancelBtn.setPrefWidth(120.0);
        cancelBtn.setStyle("-fx-font-size: 20;-fx-font-weight: bold;");
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
        okBtn.setOnMouseClicked((mouseEvent) -> {
            if (customerComboBox.getSelectionModel().getSelectedIndex() < 0) {
                (new CustomerAlert(AlertType.WARNING, "必须选择级别!", new ButtonType[0])).showAndWait();
            } else if (!StringUtils.isBlank(textField.getText()) && pattern.matcher(textField.getText()).matches()) {
                int count = Integer.parseInt(textField.getText());
                PigGradingNode pigGradingNode = new PigGradingNode();
                pigGradingNode.setBillNo("20230428GYS00001");
                pigGradingNode.setWeightProperty(new BigDecimal("30.35"));
                pigGradingNode.setGradingProperty(((KeyValuePair)customerComboBox.getSelectionModel().getSelectedItem()).getKeyPair());
                pigGradingNode.setBarCode("2023042800001");
                BizEvent bizEvent = new BizEvent("marking_" + count, pigGradingNode);
                dialogPane.getScene().getWindow().hide();
                this.actionHandler(bizEvent);
            } else {
                (new CustomerAlert(AlertType.WARNING, "必须输入打印数量且为整数!", new ButtonType[0])).showAndWait();
            }
        });
        cancelBtn.setOnMouseClicked((mouseEvent) -> {
            dialogPane.getScene().getWindow().hide();
        });
        hBox.getChildren().add(okBtn);
        hBox.getChildren().add(cancelBtn);
        hBox.prefWidthProperty().bind(gridPane.prefWidthProperty());
        gridPane.add(hBox, 0, 2, 2, 1);
        dialogPane.setContent(gridPane);
        CommonDialog.loadDialogPane(dialogPane, "批量打印").showAndWait();
    }

    private void initGradingPrint() {
        InputStream inputStream = null;

        try {
            inputStream = FileResourceLoader.loadInputStream("file:./template/gradingtickers.xml");
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(inputStream);
            document.add(new DefaultProcessingInstruction("import", "javafx.scene.control.Label"));
            document.add(new DefaultProcessingInstruction("import", "javafx.scene.layout.AnchorPane"));
            document.add(new DefaultProcessingInstruction("import", "javafx.scene.text.Font"));
            document.add(new DefaultProcessingInstruction("import", "com.ysy.tz.core.selfcomp.QRCodeImageView"));
            document.add(new DefaultProcessingInstruction("import", "com.ysy.tz.core.selfcomp.BarCodeImageView"));
            Element rootElement = document.getRootElement();
            rootElement.setName("AnchorPane");
            Element printSettingNode = rootElement.element("PrintSetting");
            this.printStopTime = Integer.parseInt(printSettingNode.attribute("stopTime").getValue());
            this.printDpi = Integer.parseInt(printSettingNode.attribute("dpi").getValue());
            this.paperWidthMM = Integer.parseInt(printSettingNode.attribute("paperWidthMM").getValue());
            this.paperHeightMM = Integer.parseInt(printSettingNode.attribute("paperHeightMM").getValue());
            rootElement.remove(printSettingNode);
            rootElement.elements().forEach((element) -> {
                Attribute attribute = element.attribute("left");
                if (attribute != null) {
                    element.remove(attribute);
                    element.addAttribute("AnchorPane.leftAnchor", attribute.getValue());
                }

                attribute = element.attribute("top");
                if (attribute != null) {
                    element.remove(attribute);
                    element.addAttribute("AnchorPane.topAnchor", attribute.getValue());
                }

            });
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            XMLWriter xmlWriter = new XMLWriter(stream) {
                public void write(Document doc) throws IOException {
                    this.writeDeclaration();
                    if (doc.getDocType() != null) {
                        this.indent();
                        this.writeDocType(doc.getDocType());
                    }

                    int i = 0;
                    ArrayList<Node> nodeList = new ArrayList();

                    Node node;
                    for(int size = doc.nodeCount(); i < size; ++i) {
                        node = doc.node(i);
                        if (node instanceof ProcessingInstruction) {
                            this.writeNode(node);
                            this.writePrintln();
                        } else {
                            nodeList.add(node);
                        }
                    }

                    Iterator var6 = nodeList.iterator();

                    while(var6.hasNext()) {
                        node = (Node)var6.next();
                        this.writeNode(node);
                    }

                    this.writePrintln();
                    this.flush();
                }
            };
            xmlWriter.write(document);
            byte[] strBytes = stream.toByteArray();
            inputStream = new ByteArrayInputStream(strBytes);
        } catch (Exception var11) {
            var11.printStackTrace();

            try {
                inputStream = FileResourceLoader.loadInputStream("/views/printpages/GradingTickets.fxml");
            } catch (IOException var10) {
                var10.printStackTrace();
            }
        }

        try {
            this.gradingPrintRegion = (Pane)(new CustomerFXMLLoader()).load((InputStream)inputStream);
        } catch (IOException var9) {
            var9.printStackTrace();
        }

    }

    @Subscribe
    public void actionHandler(BizEvent bizEvent) {
        if (bizEvent.getAction().startsWith("marking")) {
            int count = bizEvent.getAction().length() < 8 ? 1 : Integer.parseInt(bizEvent.getAction().substring(8));

            for(int i = 0; i < count; ++i) {
                try {
                    this.printQueue.put(bizEvent.getData());
                } catch (InterruptedException var5) {
                    var5.printStackTrace();
                }
            }
        }

    }

    public void changeGradingLevel(MouseEvent mouseEvent) {
        if (this.currentImitateWorker != null) {
            this.currentImitateWorker.setPause(true);
        }

        this.pathTransition.pause();
        if (this.pigPane.getSelectNode() == null) {
            (new CustomerAlert(AlertType.WARNING, "请选择一条记录!", new ButtonType[0])).showAndWait();
            if (this.currentImitateWorker != null) {
                this.currentImitateWorker.setPause(false);
            }

            this.pathTransition.play();
            this.changeLevelBtn.setSelected(false);
        } else {
            this.changePigGradingNode = this.pigPane.getSelectNode();
            System.out.println("---------->>>" + this.changePigGradingNode.actionPropertyProperty().get());
            PigGradingAction pigGradingAction = (PigGradingAction)this.changePigGradingNode.actionPropertyProperty().get();
            if (pigGradingAction == null || !pigGradingAction.getActionCode().equals("weight")) {
                (new CustomerAlert(AlertType.WARNING, "<TextFlow><Text>只有 </Text><Text style=\"-fx-font-size:14;-fx-stroke: blue;-fx-font-weight: normal;\">称重</Text><Text> 时才能 修改级别!</Text></TextFlow>", new ButtonType[0])).showAndWait();
                if (this.currentImitateWorker != null) {
                    this.currentImitateWorker.setPause(false);
                }

                this.pathTransition.play();
                this.changeLevelBtn.setSelected(false);
                return;
            }

            if (this.changeLevelDialogPane == null) {
                BorderPane borderPane = new BorderPane();
                this.changeLevelDialogPane = new CustomerDialogPane();
                this.changeLevelDialogPane.setPrefSize(650.0, 320.0);
                GridPane gridPane = new GridPane();
                ColumnConstraints columnConstraints1 = new ColumnConstraints();
                ColumnConstraints columnConstraints2 = new ColumnConstraints();
                columnConstraints2.setHgrow(Priority.ALWAYS);
                gridPane.getColumnConstraints().addAll(new ColumnConstraints[]{columnConstraints1, columnConstraints2});
                gridPane.setHgap(15.0);
                gridPane.setVgap(15.0);
                Font font = new Font(30.0);
                Label supplierLabel = new Label("供应商");
                supplierLabel.setFont(font);
                supplierLabel.setPrefWidth(100.0);
                supplierLabel.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(supplierLabel, 0, 0);
                this.supplierNameLabel = new Label();
                this.supplierNameLabel.setFont(font);
                this.supplierNameLabel.setAlignment(Pos.CENTER_LEFT);
                gridPane.add(this.supplierNameLabel, 1, 0);
                Label billLabel = new Label("单号");
                billLabel.setFont(font);
                billLabel.setPrefWidth(80.0);
                billLabel.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(billLabel, 0, 1);
                this.billNoLabel = new Label();
                this.billNoLabel.setFont(font);
                this.billNoLabel.setAlignment(Pos.CENTER_LEFT);
                gridPane.add(this.billNoLabel, 1, 1);
                Label weightLabel = new Label("重量");
                weightLabel.setFont(font);
                weightLabel.setPrefWidth(80.0);
                weightLabel.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(weightLabel, 0, 2);
                this.weightValueLabel = new Label();
                this.weightValueLabel.setFont(font);
                this.weightValueLabel.setTextFill(Color.rgb(255, 0, 0));
                this.weightValueLabel.setAlignment(Pos.CENTER_LEFT);
                gridPane.add(this.weightValueLabel, 1, 2);
                Label gradingLevelLabel = new Label("级别");
                gradingLevelLabel.setFont(font);
                gradingLevelLabel.setPrefWidth(80.0);
                gradingLevelLabel.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(gradingLevelLabel, 0, 3);
                this.flowPane = new FlowPane();
                this.flowPane.setHgap(5.0);
                Iterator var12 = this.gradingRuleGroups.values().iterator();

                while(var12.hasNext()) {
                    GradingRuleGroup gradingRuleGroup = (GradingRuleGroup)var12.next();
                    CustomerButton customerButton = new CustomerButton();
                    customerButton.setStyle("-fx-font-size: 38;");
                    customerButton.setModel(gradingRuleGroup);
                    this.flowPane.getChildren().add(customerButton);
                }

                GradingRuleGroup gradingRuleGroup = new GradingRuleGroup("Y");
                CustomerButton customerButton = new CustomerButton();
                customerButton.setStyle("-fx-font-size: 38;");
                customerButton.setModel(gradingRuleGroup);
                this.flowPane.getChildren().add(customerButton);
                gradingRuleGroup = new GradingRuleGroup("J");
                customerButton = new CustomerButton();
                customerButton.setStyle("-fx-font-size: 38;");
                customerButton.setModel(gradingRuleGroup);
                this.flowPane.getChildren().add(customerButton);
                GridPane.setHgrow(this.flowPane, Priority.ALWAYS);
                gridPane.add(this.flowPane, 1, 3, 1, 1);
                Iterator var22 = this.flowPane.getChildren().iterator();

                while(var22.hasNext()) {
                    javafx.scene.Node node = (javafx.scene.Node)var22.next();
                    CustomerButton btn = (CustomerButton)node;
                    btn.setOnMouseClicked((event) -> {
                        this.changePigGradingNode.getGradingDetailEntity().setOriginalLevelCode(this.changePigGradingNode.getGradingProperty());
                        this.changePigGradingNode.setGradingProperty(((GradingRuleGroup)btn.getModel()).getKeyPair());
                        this.updateDB(this.changePigGradingNode, "changeLevel");
                        this.changeLevelDialogPane.getScene().getWindow().hide();
                    });
                }

                borderPane.setCenter(gridPane);
                this.changeLevelDialogPane.setContent(borderPane);
            }

            this.supplierNameLabel.setText(this.changePigGradingNode.getSupplier().getSupplierName() + "(" + this.changePigGradingNode.getSupplier().getSupplierCode() + ")");
            this.billNoLabel.setText(this.changePigGradingNode.getBillNo());
            this.weightValueLabel.setText(this.changePigGradingNode.getWeightProperty().toString());
            Iterator var17 = this.flowPane.getChildren().iterator();

            while(var17.hasNext()) {
                javafx.scene.Node node = (javafx.scene.Node)var17.next();
                final CustomerButton customerButton = (CustomerButton)node;
                if (this.changePigGradingNode.getGradingProperty().equals(((GradingRuleGroup)customerButton.getModel()).getLevelCode())) {
                    Platform.runLater(new Runnable() {
                        public void run() {
                            customerButton.fireEvent(new MouseEvent(MouseEvent.MOUSE_PRESSED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1, false, false, false, false, true, false, false, false, false, false, (PickResult)null));
                            customerButton.fireEvent(new MouseEvent(MouseEvent.MOUSE_RELEASED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1, false, false, false, false, true, false, false, false, false, false, (PickResult)null));
                        }
                    });
                    break;
                }
            }

            CommonDialog.loadDialogPane(this.changeLevelDialogPane, "修改级别").showAndWait();
            this.changeLevelBtn.setSelected(false);
            if (this.currentImitateWorker != null) {
                this.currentImitateWorker.setPause(false);
            }

            this.pathTransition.play();
        }

    }

    public void changeGradingLevelAndPrint(MouseEvent mouseEvent) {
        if (this.currentImitateWorker != null) {
            this.currentImitateWorker.setPause(true);
        }

        this.pathTransition.pause();
        if (this.pigPane.getSelectNode() == null) {
            (new CustomerAlert(AlertType.WARNING, "请选择一条记录!", new ButtonType[0])).showAndWait();
            if (this.currentImitateWorker != null) {
                this.currentImitateWorker.setPause(false);
            }

            this.pathTransition.play();
            this.changeLevelBtn.setSelected(false);
        } else {
            this.changePigGradingNode = this.pigPane.getSelectNode();
            PigGradingAction pigGradingAction = this.changePigGradingNode.getActionProperty();
            if (pigGradingAction == null || !pigGradingAction.getActionCode().equals("marking")) {
                (new CustomerAlert(AlertType.WARNING, "<TextFlow><Text>只有 </Text><Text style=\"-fx-font-size:14;-fx-stroke: blue;-fx-font-weight: normal;\">打码</Text><Text> 时才能 核对改级打印!</Text></TextFlow>", new ButtonType[0])).showAndWait();
                if (this.currentImitateWorker != null) {
                    this.currentImitateWorker.setPause(false);
                }

                this.pathTransition.play();
                this.changeLevelBtn.setSelected(false);
                return;
            }

            if (this.changeGradingLevelAndPrint == null) {
                BorderPane borderPane = new BorderPane();
                this.changeGradingLevelAndPrint = new CustomerDialogPane();
                this.changeGradingLevelAndPrint.setPrefSize(650.0, 320.0);
                GridPane gridPane = new GridPane();
                ColumnConstraints columnConstraints1 = new ColumnConstraints();
                ColumnConstraints columnConstraints2 = new ColumnConstraints();
                columnConstraints2.setHgrow(Priority.ALWAYS);
                gridPane.getColumnConstraints().addAll(new ColumnConstraints[]{columnConstraints1, columnConstraints2});
                gridPane.setHgap(15.0);
                gridPane.setVgap(15.0);
                Font font = new Font(30.0);
                Label supplierLabel = new Label("供应商");
                supplierLabel.setFont(font);
                supplierLabel.setPrefWidth(100.0);
                supplierLabel.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(supplierLabel, 0, 0);
                this.supplierNameLabel = new Label();
                this.supplierNameLabel.setFont(font);
                this.supplierNameLabel.setAlignment(Pos.CENTER_LEFT);
                gridPane.add(this.supplierNameLabel, 1, 0);
                Label billLabel = new Label("单号");
                billLabel.setFont(font);
                billLabel.setPrefWidth(80.0);
                billLabel.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(billLabel, 0, 1);
                this.billNoLabel = new Label();
                this.billNoLabel.setFont(font);
                this.billNoLabel.setAlignment(Pos.CENTER_LEFT);
                gridPane.add(this.billNoLabel, 1, 1);
                Label weightLabel = new Label("重量");
                weightLabel.setFont(font);
                weightLabel.setPrefWidth(80.0);
                weightLabel.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(weightLabel, 0, 2);
                this.weightValueLabel = new Label();
                this.weightValueLabel.setFont(font);
                this.weightValueLabel.setTextFill(Color.rgb(255, 0, 0));
                this.weightValueLabel.setAlignment(Pos.CENTER_LEFT);
                gridPane.add(this.weightValueLabel, 1, 2);
                Label gradingLevelLabel = new Label("级别");
                gradingLevelLabel.setFont(font);
                gradingLevelLabel.setPrefWidth(80.0);
                gradingLevelLabel.setAlignment(Pos.CENTER_RIGHT);
                gridPane.add(gradingLevelLabel, 0, 3);
                this.flowPane = new FlowPane();
                this.flowPane.setHgap(5.0);
                Iterator var12 = this.gradingRuleGroups.values().iterator();

                while(var12.hasNext()) {
                    GradingRuleGroup gradingRuleGroup = (GradingRuleGroup)var12.next();
                    CustomerButton customerButton = new CustomerButton();
                    customerButton.setStyle("-fx-font-size: 38;");
                    customerButton.setModel(gradingRuleGroup);
                    this.flowPane.getChildren().add(customerButton);
                }

                GradingRuleGroup gradingRuleGroup = new GradingRuleGroup("Y");
                CustomerButton customerButton = new CustomerButton();
                customerButton.setStyle("-fx-font-size: 38;");
                customerButton.setModel(gradingRuleGroup);
                this.flowPane.getChildren().add(customerButton);
                gradingRuleGroup = new GradingRuleGroup("J");
                customerButton = new CustomerButton();
                customerButton.setStyle("-fx-font-size: 38;");
                customerButton.setModel(gradingRuleGroup);
                this.flowPane.getChildren().add(customerButton);
                GridPane.setHgrow(this.flowPane, Priority.ALWAYS);
                gridPane.add(this.flowPane, 1, 3, 1, 1);
                Iterator var22 = this.flowPane.getChildren().iterator();

                while(var22.hasNext()) {
                    javafx.scene.Node node = (javafx.scene.Node)var22.next();
                    CustomerButton btn = (CustomerButton)node;
                    btn.setOnMouseClicked((event) -> {
                        this.changePigGradingNode.getGradingDetailEntity().setOriginalLevelCode(this.changePigGradingNode.getGradingProperty());
                        this.changePigGradingNode.setGradingProperty(((GradingRuleGroup)btn.getModel()).getKeyPair());
                        this.updateDB(this.changePigGradingNode, "changeLevel");
                        this.changeGradingLevelAndPrint.getScene().getWindow().hide();
                        BizEvent bizEvent = new BizEvent("marking", this.changePigGradingNode);
                        this.actionHandler(bizEvent);
                    });
                }

                borderPane.setCenter(gridPane);
                this.changeGradingLevelAndPrint.setContent(borderPane);
            }

            this.supplierNameLabel.setText(this.changePigGradingNode.getSupplier().getSupplierName() + "(" + this.changePigGradingNode.getSupplier().getSupplierCode() + ")");
            this.billNoLabel.setText(this.changePigGradingNode.getBillNo());
            this.weightValueLabel.setText(this.changePigGradingNode.getWeightProperty().toString());
            Iterator var17 = this.flowPane.getChildren().iterator();

            while(var17.hasNext()) {
                javafx.scene.Node node = (javafx.scene.Node)var17.next();
                final CustomerButton customerButton = (CustomerButton)node;
                if (this.changePigGradingNode.getGradingProperty().equals(((GradingRuleGroup)customerButton.getModel()).getLevelCode())) {
                    Platform.runLater(new Runnable() {
                        public void run() {
                            customerButton.fireEvent(new MouseEvent(MouseEvent.MOUSE_PRESSED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1, false, false, false, false, true, false, false, false, false, false, (PickResult)null));
                            customerButton.fireEvent(new MouseEvent(MouseEvent.MOUSE_RELEASED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1, false, false, false, false, true, false, false, false, false, false, (PickResult)null));
                        }
                    });
                    break;
                }
            }

            CommonDialog.loadDialogPane(this.changeGradingLevelAndPrint, "核对改级打印").showAndWait();
            if (this.currentImitateWorker != null) {
                this.currentImitateWorker.setPause(false);
            }

            this.pathTransition.play();
        }

    }

    private void addEmptyWeightPigGradingNode(PigGradingNode pigGradingNode, BigDecimal weight) {
        if (pigGradingNode.isEmpty()) {
            System.out.println("----------->>> 界面上增加了一头空的!! ");
            this.pigPane.add(pigGradingNode);
            this.pigPane.requestLayout();
        } else {
            this.maxBillNo = this.getMaxBillNo(this.billNoTextField.getText(), ((SupplierEntity)this.supplierTextField.getModel()).getSupplierCode());
            this.billNoTextField.setText(this.maxBillNo);
            String barCode = ((String)SessionUtils.getAttribute(SessionUtils.WORK_DAY)).replaceAll("-", "") + this.decimalFormat.format(this.maxSerialNum);
            this.barcodeTextField.setText(barCode);
            this.weightTextField.setText(weight.toString());
            pigGradingNode.supplierProperty().set((SupplierEntity)this.supplierTextField.getModel());
            pigGradingNode.billNoProperty().set(this.maxBillNo);
            pigGradingNode.setBillNoValue(new String[]{this.maxBillNo.substring(0, 8), this.maxBillNo.substring(8)});
            Integer var4 = this.maxSerialNum;
            Integer var5 = this.maxSerialNum = this.maxSerialNum + 1;
            pigGradingNode.setSerialNumProperty(var4);
            pigGradingNode.setSeqNumProperty(Integer.parseInt(this.maxBillNo.substring(this.maxBillNo.length() - 5)));
            pigGradingNode.weightPropertyProperty().set(weight);
            pigGradingNode.barCodeProperty().set(barCode);
            if (this.bapiBtn.isSelected()) {
                pigGradingNode.getGradingDetailEntity().setSlaughterType("B");
            }

            if (this.tuimaoBtn.isSelected()) {
                pigGradingNode.getGradingDetailEntity().setSlaughterType("T");
            }

            if (this.maleBtn.isSelected()) {
                pigGradingNode.getGradingDetailEntity().setGender("G");
            }

            if (this.femaleBtn.isSelected()) {
                pigGradingNode.getGradingDetailEntity().setGender("M");
            }

            if (this.maoqiangBtn.isSelected()) {
                pigGradingNode.getGradingDetailEntity().setMjType("M");
            }

            if (this.jingqiangBtn.isSelected()) {
                pigGradingNode.getGradingDetailEntity().setMjType("J");
            }

            this.setGradingRule(pigGradingNode, weight);
            System.out.println("----------->>> 界面上增加了一头有重量的!! ");
            this.pigPane.add(pigGradingNode);
            this.pigPane.requestLayout();
            this.insertDB(pigGradingNode);
        }

    }

    private void addWeightPigGradingNode(PigGradingNode pigGradingNode, BigDecimal weight, boolean needAdd) {
        this.maxBillNo = this.getMaxBillNo(this.billNoTextField.getText(), ((SupplierEntity)this.supplierTextField.getModel()).getSupplierCode());
        this.billNoTextField.setText(this.maxBillNo);
        String barCode = ((String)SessionUtils.getAttribute(SessionUtils.WORK_DAY)).replaceAll("-", "") + this.decimalFormat.format(this.maxSerialNum);
        this.barcodeTextField.setText(barCode);
        this.weightTextField.setText(weight.toString());
        pigGradingNode.supplierProperty().set((SupplierEntity)this.supplierTextField.getModel());
        pigGradingNode.billNoProperty().set(this.maxBillNo);
        pigGradingNode.setBillNoValue(new String[]{this.maxBillNo.substring(0, 8), this.maxBillNo.substring(8)});
        Integer var5 = this.maxSerialNum;
        Integer var6 = this.maxSerialNum = this.maxSerialNum + 1;
        pigGradingNode.setSerialNumProperty(var5);
        pigGradingNode.setSeqNumProperty(Integer.parseInt(this.maxBillNo.substring(this.maxBillNo.length() - 5)));
        pigGradingNode.weightPropertyProperty().set(weight);
        pigGradingNode.barCodeProperty().set(barCode);
        if (this.bapiBtn.isSelected()) {
            pigGradingNode.getGradingDetailEntity().setSlaughterType("B");
        }

        if (this.tuimaoBtn.isSelected()) {
            pigGradingNode.getGradingDetailEntity().setSlaughterType("T");
        }

        if (this.maleBtn.isSelected()) {
            pigGradingNode.getGradingDetailEntity().setGender("G");
        }

        if (this.femaleBtn.isSelected()) {
            pigGradingNode.getGradingDetailEntity().setGender("M");
        }

        if (this.maoqiangBtn.isSelected()) {
            pigGradingNode.getGradingDetailEntity().setMjType("M");
        }

        if (this.jingqiangBtn.isSelected()) {
            pigGradingNode.getGradingDetailEntity().setMjType("J");
        }

        this.setGradingRule(pigGradingNode, weight);
        if (needAdd) {
            System.out.println("----------->>> 界面上增加了一头!! ");
            this.pigPane.push(pigGradingNode);
            this.pigPane.requestLayout();
        }

        this.insertDB(pigGradingNode);
    }

    private void setGradingRule(PigGradingNode pigGradingNode, BigDecimal weight) {
        Iterator var3 = this.gradingRuleBox.getChildren().iterator();

        while(var3.hasNext()) {
            javafx.scene.Node node = (javafx.scene.Node)var3.next();
            CustomerButton customerButton = (CustomerButton)node;
            GradingRuleGroup gradingRuleGroup = (GradingRuleGroup)customerButton.getModel();
            Iterator var7 = gradingRuleGroup.getGradingRuleEntitys().iterator();

            while(var7.hasNext()) {
                GradingRuleEntity gradingRuleEntity = (GradingRuleEntity)var7.next();
                BigDecimal startDecimal = new BigDecimal(gradingRuleEntity.getWeightStart());
                BigDecimal endDecimal = new BigDecimal(gradingRuleEntity.getWeightEnd());
                if (pigGradingNode.getGradingDetailEntity().getSlaughterType().equals(gradingRuleEntity.getSlaughterType()) && pigGradingNode.getGradingDetailEntity().getMjType().equals(gradingRuleEntity.getMjType()) && weight.compareTo(startDecimal) > 0 && weight.compareTo(endDecimal) <= 0) {
                    customerButton.fireEvent(new MouseEvent(MouseEvent.MOUSE_PRESSED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1, false, false, false, false, true, false, false, false, false, false, (PickResult)null));
                    customerButton.fireEvent(new MouseEvent(MouseEvent.MOUSE_RELEASED, 0.0, 0.0, 0.0, 0.0, MouseButton.PRIMARY, 1, false, false, false, false, true, false, false, false, false, false, (PickResult)null));
                    pigGradingNode.setGradingProperty(gradingRuleEntity.getLevelCode());
                    return;
                }
            }
        }

    }

    private void updateDB(PigGradingNode pigGradingNode, String action) {
        GradingDetailEntity gradingDetailEntity = pigGradingNode.getGradingDetailEntity();
        gradingDetailEntity.setBillNo(pigGradingNode.getBillNo());
        gradingDetailEntity.setLevelCode(pigGradingNode.getGradingProperty());
        gradingDetailEntity.setSeqNum(pigGradingNode.getSeqNumProperty());
        if (action.equals("changeLevel") && pigGradingNode.getActionProperty().getActionCode().equals("marking")) {
            gradingDetailEntity.setChangeLevelWhenPrint("Y");
        }

        gradingDetailEntity.setGender(pigGradingNode.getGradingDetailEntity().getGender());
        gradingDetailEntity.setSlaughterType(pigGradingNode.getGradingDetailEntity().getSlaughterType());
        gradingDetailEntity.setMjType(pigGradingNode.getGradingDetailEntity().getMjType());

        try {
            this.pigGradingService.updateGradingRecord(gradingDetailEntity);
        } catch (Exception var5) {
            var5.printStackTrace();
            (new CustomerAlert(AlertType.ERROR, "修改数据库失败,请联系管理员！", new ButtonType[0])).showAndWait();
        }

        int maxSeqNo = Integer.parseInt(this.maxBillNo.substring(this.maxBillNo.length() - 5));
        if (pigGradingNode.getSeqNumProperty() > maxSeqNo) {
            this.maxBillNo = this.maxBillNo.substring(0, this.maxBillNo.length() - 5) + this.decimalFormat.format((long)pigGradingNode.getSeqNumProperty());
            this.billNoTextField.setText(this.maxBillNo);
        }

    }

    private void insertDB(PigGradingNode pigGradingNode) {
        GradingDetailEntity gradingDetailEntity = pigGradingNode.getGradingDetailEntity();
        gradingDetailEntity.setSupplierId(((SupplierEntity)this.supplierTextField.getModel()).getSupplierId());
        gradingDetailEntity.setBillNo(pigGradingNode.getBillNo());
        gradingDetailEntity.setWeight(pigGradingNode.getWeightProperty());
        gradingDetailEntity.setLevelCode(pigGradingNode.getGradingProperty());
        gradingDetailEntity.setSerialNum(pigGradingNode.getSerialNumProperty());
        gradingDetailEntity.setSeqNum(pigGradingNode.getSeqNumProperty());
        gradingDetailEntity.setBarcode(pigGradingNode.getBarCode());

        try {
            this.pigGradingService.createGradingRecord(gradingDetailEntity, ((WarehouseEntity)this.warehouseTextField.getModel()).getWarehouseId());
        } catch (Exception var4) {
            var4.printStackTrace();
            (new CustomerAlert(AlertType.ERROR, "插入数据库失败,请联系管理员！", new ButtonType[0])).showAndWait();
        }

    }

    private String getMaxBillNo(String maxBillNo, String supplierCode) {
        if (StringUtils.isNotBlank(maxBillNo)) {
            String seqNoStr = maxBillNo.substring(maxBillNo.length() - 5);
            int seqNo = Integer.parseInt(seqNoStr);
            StringBuilder var10000 = (new StringBuilder()).append(maxBillNo.substring(0, maxBillNo.length() - 5));
            ++seqNo;
            return var10000.append(this.decimalFormat.format((long)seqNo)).toString();
        } else {
            return ((String)SessionUtils.getAttribute(SessionUtils.WORK_DAY)).replaceAll("-", "") + supplierCode + "00000";
        }
    }

    private void startLoopPane(Pane pane, ImageView imageView) {
        pane.setClip(new Rectangle(pane.getPrefWidth(), pane.getPrefHeight()));
        Path path = new Path();
        path.getElements().add(new MoveTo(0.0, pane.getPrefHeight() / 2.0));
        path.getElements().add(new HLineTo(pane.getPrefWidth()));
        this.pathTransition = new PathTransition();
        this.pathTransition.setDuration(Duration.millis(2000.0));
        this.pathTransition.setPath(path);
        this.pathTransition.setNode(imageView);
        this.pathTransition.setOrientation(OrientationType.ORTHOGONAL_TO_TANGENT);
        this.pathTransition.setInterpolator(Interpolator.EASE_IN);
        this.pathTransition.setCycleCount(-1);
        this.pathTransition.play();
    }

    public void addPigPane() {
        PigGradingOldNode pigGradingNode = new PigGradingOldNode();
        pigGradingNode.setEmpty(true);
        this.pigPane.push(pigGradingNode);
    }

    private class ImitateWorker implements Runnable {
        private volatile boolean running = true;
        private volatile boolean pause = false;
        private int interval;
        Random random = new Random();
        String[] ws = new String[]{"35", "0", "36", "-2.4", "38", "0", "42", "0"};
        volatile int count = 0;

        public ImitateWorker(int interval) {
            this.interval = interval;
        }

        public void start() {
            ApplicationContext.executor.execute(this);
        }

        public void setPause(boolean pause) {
            this.pause = pause;
        }

        public void stop() {
            this.running = false;
        }

        public void run() {
            while(this.running) {
                try {
                    if (!this.pause) {
                        double weight = Double.valueOf(this.ws[this.count]);
                        PigGradingNode pigGradingNode;
                        if (weight < 3.0) {
                            pigGradingNode = new PigGradingNode();
                            pigGradingNode.setEmpty(true);
                            Platform.runLater(() -> {
                                PigGradingController.this.addEmptyWeightPigGradingNode(pigGradingNode, new BigDecimal(weight));
                            });
                        } else {
                            pigGradingNode = new PigGradingNode();
                            Platform.runLater(() -> {
                                PigGradingController.this.addEmptyWeightPigGradingNode(pigGradingNode, new BigDecimal(weight));
                            });
                        }

                        ++this.count;
                        if (this.count >= this.ws.length - 1) {
                            this.count = 0;
                        }
                    }

                    Thread.sleep((long)this.interval);
                } catch (InterruptedException var4) {
                    var4.printStackTrace();
                }
            }

        }
    }

    class PrintWorkThread extends Thread {
        PrintWorkThread() {
        }

        public void run() {
            while(!Thread.currentThread().isInterrupted()) {
                try {
                    PigGradingNode pigGradingNode = (PigGradingNode)PigGradingController.this.printQueue.take();
                    Platform.runLater(() -> {
                        try {
                            UserEntity userEntity = (UserEntity)SessionUtils.getAttribute(SessionUtils.CURRENT_USER);
                            Iterator var3 = PigGradingController.this.gradingPrintRegion.getChildren().iterator();

                            while(var3.hasNext()) {
                                javafx.scene.Node node = (javafx.scene.Node)var3.next();
                                Label textField;
                                if (node.getId() != null && node.getId().equals("level")) {
                                    textField = (Label)node;
                                    textField.setText(pigGradingNode.getGradingProperty());
                                }

                                if (node.getId() != null && node.getId().equals("zhijianyuan")) {
                                    textField = (Label)node;
                                    textField.setText("质检员：" + userEntity.getUserCode());
                                }

                                if (node.getId() != null && node.getId().equals("barcode")) {
                                    BarCodeImageView barCodeImageView = (BarCodeImageView)node;
                                    barCodeImageView.setBarCodeValue(pigGradingNode.getBarCode());
                                }

                                if (node.getId() != null && node.getId().equals("qrcode")) {
                                    QRCodeImageView barCodeImageViewx = (QRCodeImageView)node;
                                    if (PigGradingController.this.oldQRValue == null) {
                                        PigGradingController.this.oldQRValue = barCodeImageViewx.getQRValue();
                                    }

                                    barCodeImageViewx.setQRValue(PigGradingController.this.oldQRValue + "?" + pigGradingNode.getBarCode());
                                }
                            }

                            if (PigGradingController.this.gradingPrintRegion.getScene() == null) {
                                new Scene(PigGradingController.this.gradingPrintRegion, PigGradingController.this.gradingPrintRegion.getPrefWidth(), PigGradingController.this.gradingPrintRegion.getPrefHeight());
                            } else {
                                AnchorPane anchorPane = new AnchorPane();
                                anchorPane.getChildren().addAll(PigGradingController.this.gradingPrintRegion.getChildren());
                                anchorPane.setPrefWidth(PigGradingController.this.gradingPrintRegion.getPrefWidth());
                                anchorPane.setPrefHeight(PigGradingController.this.gradingPrintRegion.getPrefHeight());
                                new Scene(anchorPane, PigGradingController.this.gradingPrintRegion.getPrefWidth(), PigGradingController.this.gradingPrintRegion.getPrefHeight());
                                PigGradingController.this.gradingPrintRegion = anchorPane;
                            }

                            System.out.println("区域大小 停1 --->>> " + PigGradingController.this.gradingPrintRegion.getChildren().size() + "," + PigGradingController.this.gradingPrintRegion.getScene().getWidth() + "," + PigGradingController.this.gradingPrintRegion.getScene().getHeight());
                            PrintUtils.printNode(PigGradingController.this.gradingPrintRegion, PigGradingController.this.paperWidthMM, PigGradingController.this.paperHeightMM, PigGradingController.this.printDpi, 0);
                        } catch (Exception var6) {
                            var6.printStackTrace();
                        }

                    });
                    Thread.sleep((long)PigGradingController.this.printStopTime);
                } catch (InterruptedException var2) {
                    var2.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            }

        }
    }
}
